﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class Home_Message : System.Web.UI.Page
{
    UserEntity currentUser;
    public LanguageManage Language;
    private UserAdapter userAdapter;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check login
        currentUser = UserManager.Instance.Getuser();
        if (!currentUser.isLogin)
            Response.Redirect("~/Login.aspx");

        Language = new LanguageManage();
        userAdapter = new UserAdapter();
        Session["menu_id"] = "message";
        if (!IsPostBack)
        {
            ComboUser.DataSource = userAdapter.getAllUser();
            ComboUser.DataTextField = "Account";
            ComboUser.DataValueField = "Mail";
            ComboUser.DataBind();
            ComboUser.Items.Add("All");
            ComboUser.SelectedIndex = ComboUser.Items.Count - 1;
        }
        btn_create.Text = Language["SendMail"];
    }
    protected void btn_create_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        try
        {
            if (ComboUser.Text.Equals("All"))
            {
                List<UserEntity> users = userAdapter.getAllUser();
                foreach (UserEntity user in users)
                {
                    MailMessage MyMailMessage = new MailMessage();
                    MyMailMessage.From = new MailAddress("truonggiang08x@gmail.com");
                    MyMailMessage.To.Add(user.Mail);
                    MyMailMessage.Subject = "Test Send Mail CMSBook";
                    MyMailMessage.Body = txtMessage.Text;
                    SmtpClient SMTPClient = new SmtpClient("smtp.gmail.com");
                    SMTPClient.Port = 587;
                    SMTPClient.Credentials = new System.Net.NetworkCredential("truonggiang08x@gmail.com", "abc");
                    SMTPClient.EnableSsl = true;
                    SMTPClient.Send(MyMailMessage);
                }
            }
            else
            {
                MailMessage MyMailMessage = new MailMessage();
                MyMailMessage.From = new MailAddress("truonggiang08x@gmail.com");
                MyMailMessage.To.Add(ComboUser.SelectedValue.ToString());
                MyMailMessage.Subject = "Test Send Mail CMSBook";
                MyMailMessage.Body = txtMessage.Text;
                SmtpClient SMTPClient = new SmtpClient("smtp.gmail.com");
                SMTPClient.Port = 587;
                SMTPClient.Credentials = new System.Net.NetworkCredential("truonggiang08x@gmail.com", "lngi@ng123");
                SMTPClient.EnableSsl = true;
                SMTPClient.Send(MyMailMessage);
            }
            lblMessage.Text = Language["MessageSendMail"];
        }
        catch (Exception)
        {

            throw;
        }
    }
}